package com.sqllog.admin.auth.service;


import com.sqllog.admin.auth.domain.Power;
import com.sqllog.admin.auth.domain.User;
import com.sqllog.admin.auth.mapper.PowerMapper;
import com.sqllog.admin.auth.mapper.UserMapper;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;


/**
 * Security 用户服务
 *
 * @author chennengcheng
 * @date 2022-01-14 14:35
 * @remark
 */

@Component
public class SecureUserDetailsService implements UserDetailsService {

    @Resource
    private UserMapper sysUserMapper;

    @Resource
    private PowerMapper sysPowerMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User sysUser = sysUserMapper.selectByUsername(username);
        if (sysUser == null) {
            throw new UsernameNotFoundException("Account Not Found");
        }

        List<Power> powerList = sysPowerMapper.selectAll(username);
        sysUser.setPowerList(powerList);
        return sysUser;
    }
}
